package com.config;

import com.bean.WebsiteVisitorAbnormal;
import com.mapper.repository.mysql.WebsiteVisitorAbnormalRepository;
import com.mapper.repository.mysql.WebsiteVisitorDisableRepository;

import com.utils.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.UUID;

@Slf4j
@Order(1)
@WebFilter(urlPatterns = { "/website/*", "/websiteType/*", "/message/*" , "/visitor/*",
        "/logintoken/*","/sys/*"})
public class FilterToken implements Filter {


    @Autowired
    private JWTUtil jwtUtil;

    public FilterToken() {

    }
    @Override
    public void doFilter(ServletRequest servletRequest,
                         ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //System.out.println("经过过滤器");
        String addr = request.getRemoteAddr();
        //判断 token是否有效，判断token是否需要更新
        log.info("请求的API:"+request.getRequestURI());
        //String token=request.getHeader("token");
        String token = null;
        Cookie[] cookies = request.getCookies();
        if(cookies==null){
            log.info("未登录:"+token);
            request.getRequestDispatcher("/toLoginError?error="+"未登录").forward(request,response);
        }else{
            for(Cookie cookie : cookies){
                if(cookie.getName().equals("token")){
                    token = cookie.getValue();
                }
            }
            if(token!=null){
                log.info("token:"+token);
                //判断token是否有效
                String message=jwtUtil.validateToken(token);
                if(message.equals("token有效")){
                    String newToken=jwtUtil.refreshToken(token);
                    if(newToken!=null){
                        // 创建新Cookie覆盖旧Cookie
                        Cookie cookie = new Cookie("token", newToken);
                        // 设置与原有Cookie相同的路径和域名，确保覆盖生效
                        cookie.setPath("/"); // 根路径，全网站可见
                        cookie.setHttpOnly(true);
                        response.addCookie(cookie);
                    }
                    filterChain.doFilter(request,response);
                }else{
                    //转发
                    request.getRequestDispatcher("/toLoginError?error="+message).forward(request,response);
                }
                //判断token是否过期
            }else{
                log.info("未登录:"+token);
                request.getRequestDispatcher("/toLoginError?error="+"未登录").forward(request,response);
            }
        }

    }

    @Override
    public void destroy() {

    }

}
